//
//  HMSeletCommonView.m
//  优学荟管理系统
//
//  Created by 陈辉明 on 2017/12/12.
//  Copyright © 2017年 星道三好. All rights reserved.
//

#import "HMSeletCommonView.h"
#import "HMSeletCommonCell.h"
#import "HMGetTemplatesModel.h"


@interface HMSeletCommonView() <UITableViewDelegate,UITableViewDataSource>

@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property(nonatomic,strong) NSMutableArray *dataSource;


@end

@implementation HMSeletCommonView

-(instancetype)initHMSeletCommonViewWithArr : (NSMutableArray *) dataSource {
    self = [[[NSBundle mainBundle]loadNibNamed:@"HMSeletCommonView" owner:nil options:nil]lastObject];
    self.frame = CGRectMake(0, 0, kScreenWidth, kScreenHeight);
    self.dataSource = [NSMutableArray array];
    self.dataSource = dataSource;
    UIWindow *keyWindow = [[UIApplication sharedApplication] keyWindow];
    [keyWindow addSubview:self];
    return self;
}


-(void)layoutSubviews {
    [super layoutSubviews];
     [self setUpTableView];
}

-(void)awakeFromNib {
    [super awakeFromNib];
   
}

- (void)setUpTableView {
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    self.tableView.tableFooterView = [UIView new];
    [self.tableView registerNib:[UINib nibWithNibName:@"HMSeletCommonCell" bundle:nil] forCellReuseIdentifier:@"HMSeletCommonCellId"];
    
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataSource.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    HMGetTemplatesModel *model = self.dataSource[indexPath.row];
    HMSeletCommonCell *cell = [tableView dequeueReusableCellWithIdentifier:@"HMSeletCommonCellId" forIndexPath:indexPath];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.model = model;
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    HMGetTemplatesModel *model = self.dataSource[indexPath.row];
    if (self.selectCommonBlock) {
        self.selectCommonBlock(model);
    }
    [self removeFromSuperview];
}





@end
